package com.kucai.yifaqapp.ui.fragment.gameFragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.fragment.DaggerNNFragmentComponent;
import com.kucai.yifaqapp.dagger.module.fragment.NNFragmentModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResGameDetails;
import com.kucai.yifaqapp.model.network.entity.res.ResMoney;
import com.kucai.yifaqapp.model.network.entity.res.Resmiaosuniuniu;
import com.kucai.yifaqapp.model.network.entity.res.Respk10niu;
import com.kucai.yifaqapp.model.network.entity.res.Viewbean;
import com.kucai.yifaqapp.presenter.fragment.NNFragmentPresenter;
import com.kucai.yifaqapp.ui.activity.BaseActivity;
import com.kucai.yifaqapp.ui.activity.GameDetailActivityFinal;
import com.kucai.yifaqapp.ui.fragment.BaseFragment;
import com.kucai.yifaqapp.util.DateUtils;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.SYSOUtils;
import com.kucai.yifaqapp.util.SpUtils;
import com.kucai.yifaqapp.util.TimeTools;
import com.kucai.yifaqapp.widget.KeyBoardWeight;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/12/10.
 */

public class NNFragment extends BaseFragment<NNFragmentPresenter> implements View.OnClickListener {

    @BindView(R.id.niuniu_up_tv)
    TextView niuniuUpTv;
    @BindView(R.id.kuai8_onet)
    TextView kuai8Onet;
    @BindView(R.id.kuai8_twot)
    TextView kuai8Twot;
    @BindView(R.id.kuai8_threet)
    TextView kuai8Threet;
    @BindView(R.id.kuai8_fourt)
    TextView kuai8Fourt;
    @BindView(R.id.kuai8_fivt)
    TextView kuai8Fivt;
    @BindView(R.id.kuai8_sixt)
    TextView kuai8Sixt;
    @BindView(R.id.kuai8_sevent)
    TextView kuai8Sevent;
    @BindView(R.id.kuai8_eightt)
    TextView kuai8Eightt;
    @BindView(R.id.kuai8_noont)
    TextView kuai8Noont;
    @BindView(R.id.kuai8_tent)
    TextView kuai8Tent;
    @BindView(R.id.kuai8tv)
    LinearLayout kuai8tv;
    @BindView(R.id.niuniu_tv_one)
    TextView niuniuTvOne;
    @BindView(R.id.niuniu_tv_two)
    TextView niuniuTvTwo;
    @BindView(R.id.niuniu_tv_three)
    TextView niuniuTvThree;
    @BindView(R.id.niuniu_tv_four)
    TextView niuniuTvFour;
    @BindView(R.id.niuniu_tv_fiv)
    TextView niuniuTvFiv;
    @BindView(R.id.niuniu_tv_six)
    TextView niuniuTvSix;
    @BindView(R.id.niuniu_tv_lin)
    LinearLayout niuniuTvLin;
    @BindView(R.id.period_down_tv)
    TextView periodDownTv;
    @BindView(R.id.close_time_tv)
    TextView closeTimeTv;
    @BindView(R.id.open_time_tv)
    TextView openTimeTv;
    @BindView(R.id.fiv)
    TextView fiv;
    @BindView(R.id.putonglayout)
    LinearLayout putonglayout;
    @BindView(R.id.niuniu_title)
    TextView niuniuTitle;
    @BindView(R.id.xiantv1)
    TextView xiantv1;
    @BindView(R.id.xiantv1s)
    TextView xiantv1s;
    @BindView(R.id.xiantv1lin)
    LinearLayout xiantv1lin;
    @BindView(R.id.xiantv2)
    TextView xiantv2;
    @BindView(R.id.xiantv2s)
    TextView xiantv2s;
    @BindView(R.id.xiantv2lin)
    LinearLayout xiantv2lin;
    @BindView(R.id.xiantv3)
    TextView xiantv3;
    @BindView(R.id.xiantv3s)
    TextView xiantv3s;
    @BindView(R.id.xiantv3lin)
    LinearLayout xiantv3lin;
    @BindView(R.id.xiantv4)
    TextView xiantv4;
    @BindView(R.id.xiantv4s)
    TextView xiantv4s;
    @BindView(R.id.xiantv4lin)
    LinearLayout xiantv4lin;
    @BindView(R.id.xiantv5)
    TextView xiantv5;
    @BindView(R.id.xiantv5s)
    TextView xiantv5s;
    @BindView(R.id.xiantv5lin)
    LinearLayout xiantv5lin;
    @BindView(R.id.includ5)
    LinearLayout includ5;
    @BindView(R.id.saicheRl)
    RelativeLayout saicheRl;
    @BindView(R.id.yixuanzhong)
    TextView yixuanzhong;
    @BindView(R.id.touzhued)
    EditText touzhued;
    @BindView(R.id.xiazhubt)
    Button xiazhubt;
    @BindView(R.id.chongzhibt)
    Button chongzhibt;
    @BindView(R.id.xiazhulin)
    LinearLayout xiazhulin;
    @BindView(R.id.fengpanlin)
    TextView fengpanlin;
    @BindView(R.id.keybord)
    KeyBoardWeight keybord;
    private String gameid;//游戏id
    private String money;//金额
    private String dataid;//期数
    //    private String webvideo;//期数
    private TextView tvmoney;
    private AlertDialog dialog;
    private String betBean_title1[];
    private String betBean_text1[];
    private String betBean_odds1[];
    private String betBean_money1[];
    ArrayList<String> thecrownIndex = new ArrayList<>();//选中的集合
    ArrayList<Viewbean> linearLayoutsList = new ArrayList<>();//选中的线性布局
    private Adapter adapter;

    String[] niuniu = {"无牛", "牛一", "牛二", "牛三", "牛四", "牛五", "牛六", "牛七", "牛八", "牛九", "牛牛"};

    @Override
    protected int setContentView() {
        return R.layout.niuniufragment;
    }

    @Override
    protected void initDagger() {
        DaggerNNFragmentComponent.builder()
                .nNFragmentModule(new NNFragmentModule(this))
                .build().inject(this);
    }

    @Override
    protected void ChangeSkin() {

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (closeTimeTv.getText().toString().trim().equals("已封盘")) {
            fengpanlin.setVisibility(View.VISIBLE);
            xiazhubt.setEnabled(false);
            chongzhibt.setEnabled(false);
            touzhued.setEnabled(false);
        }

        //输入框处理
//        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        touzhued.setInputType(InputType.TYPE_NULL);
        keybord.init(touzhued, new KeyBoardWeight.Goback() {
            @Override
            public void goback() {
                keybord.setVisibility(View.GONE);
            }
        });
        touzhued.setFocusable(true);
        touzhued.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                keybord.setVisibility(View.VISIBLE);
            }
        });
        touzhued.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (touzhued.getText().toString().matches("^0")) {//判断当前的输入第一个数是不是为0
                    touzhued.setText("");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
//                SYSOUtils.sOut_("wwwwwwwwwwwwwwwwwwwwww" + s);
            }
        });
        chongzhibt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "重置", Toast.LENGTH_SHORT).show();
                for (Viewbean q : linearLayoutsList) {
                    q.linearLayout.setBackgroundResource(R.drawable.betting_backgroud);
                    if (null != q.textView) {
                        q.textView.setTextColor(getContext().getResources().getColor(R.color.blank));
                    }
                    q.textViews.setTextColor(getContext().getResources().getColor(R.color.text_color));
                    thecrownIndex.remove(q + "");
                }
                thecrownIndex.clear();
                yixuanzhong.setText(thecrownIndex.size() + "");
            }
        });
        xiazhubt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeKeyBoard();
                String edmoney = touzhued.getText().toString().trim();
                if (TextUtils.isEmpty(edmoney)) {
                    Toast.makeText(getContext(), "请输入投注金额", Toast.LENGTH_SHORT).show();
                } else {

                    long Total_price = Long.parseLong(edmoney) * thecrownIndex.size();
                    String[] betBean_Play = new String[thecrownIndex.size()];
                    String[] betBean_odds = new String[thecrownIndex.size()];
                    String[] betBean_money = new String[thecrownIndex.size()];
                    String[] betBean_title = new String[thecrownIndex.size()];
                    String[] betBean_text = new String[thecrownIndex.size()];
                    if (thecrownIndex.size() != 0 && null != thecrownIndex) {
                        for (int i = 0; i < thecrownIndex.size(); i++) {
//                            SYSOUtils.sOut_("当前集合为" + linearLayoutsList.get(Integer.parseInt(thecrownIndex.get(i))));
                            betBean_Play[i] = linearLayoutsList.get(Integer.parseInt(thecrownIndex.get(i))).id;
                            betBean_money[i] = edmoney;
                            betBean_odds[i] = linearLayoutsList.get(Integer.parseInt(thecrownIndex.get(i))).textViews.getText().toString();
                            betBean_title[i] = linearLayoutsList.get(Integer.parseInt(thecrownIndex.get(i))).str;
                            if (null != linearLayoutsList.get(Integer.parseInt(thecrownIndex.get(i))).textView) {
                                betBean_text[i] = linearLayoutsList.get(Integer.parseInt(thecrownIndex.get(i))).textView.getText().toString();
                            }


                        }
                        betBean_title1 = betBean_title;
                        betBean_text1 = betBean_text;
                        betBean_odds1 = betBean_odds;
                        betBean_money1 = betBean_money;
                    } else {
                        Toast.makeText(getContext(), "您还没有下注", Toast.LENGTH_SHORT).show();
                    }


                    try {
                        if (!TextUtils.isEmpty(gameid) && !TextUtils.isEmpty(dataid) && thecrownIndex.size() != 0 && Total_price != 0) {

                            final JSONObject result = new JSONObject();
                            result.put("gameId", gameid);
                            result.put("turnNum", dataid);
                            result.put("totalNums", thecrownIndex.size() + "");
                            result.put("totalMoney", Total_price + "");
                            for (int i = 0; i < betBean_Play.length; i++) {
                                result.put("betBean_playId[" + i + "]", betBean_Play[i]);
                                result.put("betBean_odds[" + i + "]", betBean_odds[i]);
                                result.put("betBean_money[" + i + "]", betBean_money[i]);
                            }
                            SYSOUtils.sOut_("数据" + result);
//                            SYSOUtils.sOut_("游戏id" + gameid + "期数" + dataid + "总株数" + thecrownIndex.size() + "下注总金额" + Total_price);
//                            SYSOUtils.sOut_("每一注id" + betBean_Play[0] + "每一住赔率" + betBean_odds[0] + "没注金额" + betBean_money[0]);
//                            SYSOUtils.sOut_("最终json1" + result.toString());

                            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                            View view = View.inflate(getContext(), R.layout.gamepop_layout, null);
                            RecyclerView game_pop_list = (RecyclerView) view.findViewById(R.id.game_pop_list);
                            TextView game_pop_canclbt = (TextView) view.findViewById(R.id.game_pop_canclbt);
                            TextView game_pop_yesbt = (TextView) view.findViewById(R.id.game_pop_yesbt);
                            game_pop_list.setLayoutManager(new LinearLayoutManager(getContext()));
                            //创建适配器
                            adapter = new Adapter();
                            game_pop_list.setAdapter(adapter);
                            builder.setView(view);
                            dialog = builder.create();
                            dialog.setCancelable(true);//点击外边缘隐藏
                            dialog.show();
                            game_pop_canclbt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });
                            game_pop_yesbt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    mPresenter.Betbet(result.toString(), Info.user.token);
                                }
                            });

                        } else {
                            Toast.makeText(getContext(), "请选择正确投注内容", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }
        });
        //获取游戏id
        final FragmentActivity activity = getActivity();
        if (activity != null)

        {
            final GameDetailActivityFinal gameDetailsActivity = (GameDetailActivityFinal) activity;
            gameid = gameDetailsActivity.gameid;
            money = gameDetailsActivity.money;
            tvmoney = gameDetailsActivity.moneyTv;
//            webvideo = gameDetailsActivity.webvideo;
        }

        if (null != gameid) {

            //头部
            mPresenter.getgamesdetail(gameid);
            //赔率设置
            switch (Integer.parseInt(gameid)) {
                case 91:
                    //秒速牛牛
                    mPresenter.getmiaosuniuniuoddes(gameid, Info.user.token);
                    break;
                case 90:
                    //pk10牛牛
                    mPresenter.getpk10niuoddes(gameid, Info.user.token);
                    break;
            }
        }

    }


    @OnClick(R.id.fiv)
    public void onViewClicked() {

    }

    /**
     * 关闭软键盘
     */
    private void closeKeyBoard() {
        InputMethodManager imm = (InputMethodManager) touzhued.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(touzhued.getApplicationWindowToken(), 0);

        }
    }

    public void moneychange(ResMoney resMoney) {
        tvmoney.setText(resMoney.data.balance + "");
        Info.user.money = resMoney.data.balance + "";
    }

    /**
     * 下注成功
     */
    public void betsuccses() {
        Toast.makeText(getContext(), "下注成功", Toast.LENGTH_SHORT).show();
        for (Viewbean q : linearLayoutsList) {
            q.linearLayout.setBackgroundResource(R.drawable.betting_backgroud);
            if (null != q.textView) {
                q.textView.setTextColor(getContext().getResources().getColor(R.color.blank));
            }
            q.textViews.setTextColor(getContext().getResources().getColor(R.color.text_color));
            thecrownIndex.remove(q + "");
        }
        thecrownIndex.clear();
        yixuanzhong.setText(thecrownIndex.size() + "");
        keybord.setVisibility(View.GONE);
        //请求余额刷新
        if (null != Info.user) {
            mPresenter.getmoney(Info.user.token);
        }
//        touzhued.setText("");
    }

    //token过期
    public void restartlogin() {
        Info.user = null;
        SpUtils.clearUser(getContext());
        ((BaseActivity) getActivity()).startLogin();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.includ5:
                break;
            case R.id.xiantv1lin:
                break;
        }
    }


    /////////////////////////////////////////////////////////////////////////////
    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.game_popitem, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            holder.geme_tv.setText("【" + betBean_title1[position] + "-" + betBean_text1[position] + "】");
            holder.geme_tv1.setText("@" + betBean_odds1[position] + "×" + betBean_money1[position]);

            if (position + 1 == thecrownIndex.size()) {
                holder.game_pop_botton.setVisibility(View.VISIBLE);
                holder.game_pop_bottonlin.setVisibility(View.VISIBLE);
                holder.leftmoney.setText(position + 1 + "");
                holder.rightmoney.setText((position + 1) * Long.parseLong(betBean_money1[0]) + "");
            }else{
                holder.game_pop_botton.setVisibility(View.GONE);
                holder.game_pop_bottonlin.setVisibility(View.GONE);
            }
        }

        @Override
        public int getItemCount() {
            return thecrownIndex.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView geme_tv, leftmoney, rightmoney;
            TextView geme_tv1;
            View game_pop_bottonlin;
            LinearLayout game_pop_botton;

            MViewHolder(View itemView) {
                super(itemView);
                geme_tv = (TextView) itemView.findViewById(R.id.gamepoptext);
                geme_tv1 = (TextView) itemView.findViewById(R.id.gamepoptext1);
                leftmoney = (TextView) itemView.findViewById(R.id.leftmoney);
                rightmoney = (TextView) itemView.findViewById(R.id.rightmoney);
                game_pop_bottonlin = (View) itemView.findViewById(R.id.game_pop_bottonlin);
                game_pop_botton = (LinearLayout) itemView.findViewById(R.id.game_pop_botton);

            }
        }
    }

    /**
     * pk10牛牛
     */
    public void setdatapk10niuniu(Respk10niu.DataBean listdata) {
        Drawable img = fiv.getResources().getDrawable(R.mipmap.whitedots);
        // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        img.setBounds(0, 0, img.getMinimumWidth(), img.getMinimumHeight());
        fiv.setCompoundDrawables(img, null, null, null);
        xiantv1s.setText(listdata._$901903462);
        xiantv2s.setText(listdata._$901903463);
        xiantv3s.setText(listdata._$901903464);
        xiantv4s.setText(listdata._$901903465);
        xiantv5s.setText(listdata._$901903466);
        xiantv1s.setVisibility(View.INVISIBLE);
        xiantv2s.setVisibility(View.INVISIBLE);
        xiantv3s.setVisibility(View.INVISIBLE);
        xiantv4s.setVisibility(View.INVISIBLE);
        xiantv5s.setVisibility(View.INVISIBLE);
        linearLayoutsList.add(new Viewbean(xiantv1lin, xiantv1, xiantv1s, "牛牛", "901903462"));
        linearLayoutsList.add(new Viewbean(xiantv2lin, xiantv2, xiantv2s, "牛牛", "901903463"));
        linearLayoutsList.add(new Viewbean(xiantv3lin, xiantv3, xiantv3s, "牛牛", "901903464"));
        linearLayoutsList.add(new Viewbean(xiantv4lin, xiantv4, xiantv4s, "牛牛", "901903465"));
        linearLayoutsList.add(new Viewbean(xiantv5lin, xiantv5, xiantv5s, "牛牛", "901903466"));
        textcolorlist();
    }

    /**
     * 秒速牛牛
     */

    public void setdatamiaosuniuniu(Resmiaosuniuniu.DataBean listdata) {
        xiantv1s.setText(listdata._$911893457);
        xiantv2s.setText(listdata._$911893458);
        xiantv3s.setText(listdata._$911893459);
        xiantv4s.setText(listdata._$911893460);
        xiantv5s.setText(listdata._$911893461);
        xiantv1s.setVisibility(View.INVISIBLE);
        xiantv2s.setVisibility(View.INVISIBLE);
        xiantv3s.setVisibility(View.INVISIBLE);
        xiantv4s.setVisibility(View.INVISIBLE);
        xiantv5s.setVisibility(View.INVISIBLE);
        linearLayoutsList.add(new Viewbean(xiantv1lin, xiantv1, xiantv1s, "牛牛", "911893457"));
        linearLayoutsList.add(new Viewbean(xiantv2lin, xiantv2, xiantv2s, "牛牛", "911893458"));
        linearLayoutsList.add(new Viewbean(xiantv3lin, xiantv3, xiantv3s, "牛牛", "911893459"));
        linearLayoutsList.add(new Viewbean(xiantv4lin, xiantv4, xiantv4s, "牛牛", "911893460"));
        linearLayoutsList.add(new Viewbean(xiantv5lin, xiantv5, xiantv5s, "牛牛", "911893461"));
        textcolorlist();
    }

    //设置字体颜色和背景色
    private void textcolorlist() {
        for (int i = 0; i < linearLayoutsList.size(); i++) {
            final int q = i;

            linearLayoutsList.get(i).linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (thecrownIndex.contains(q + "")) {
                        thecrownIndex.remove(q + "");
                        linearLayoutsList.get(q).linearLayout.setBackgroundResource(R.drawable.betting_backgroud);
                        if (null != linearLayoutsList.get(q).textView) {
                            linearLayoutsList.get(q).textView.setTextColor(getContext().getResources().getColor(R.color.blank));
                        }
                        linearLayoutsList.get(q).textViews.setTextColor(getContext().getResources().getColor(R.color.text_color2));

                    } else {
                        linearLayoutsList.get(q).linearLayout.setBackgroundResource(R.drawable.unxiazhu);
                        linearLayoutsList.get(q).textViews.setTextColor(getContext().getResources().getColor(R.color.settingbg));
                        if (null != linearLayoutsList.get(q).textView) {
                            linearLayoutsList.get(q).textView.setTextColor(getContext().getResources().getColor(R.color.settingbg));
                        }
                        thecrownIndex.add(q + "");
                    }
                    yixuanzhong.setText(thecrownIndex.size() + "");
                }
            });

        }

    }

    //牛牛
    public void Lottery(ResGameDetails resGameDetails) {
        GameDetailActivityFinal.webvideo = resGameDetails.data.videoUrl;
//        webvideo = resGameDetails.data.videoUrl;
        if (!gameid.equals("91")) {
            niuniuUpTv.setPadding(20, 0, 0, 0);
        }
        niuniuUpTv.setText(resGameDetails.data.preIssue + "期");
        periodDownTv.setText(resGameDetails.data.issue + "期");
        startleftTime(resGameDetails.data.endTime, resGameDetails.data.serverTime);
        startrightTime(resGameDetails.data.lotteryTime, resGameDetails.data.serverTime, 1);
        String[] strarray = resGameDetails.data.preNum.split(",");
        dataid = String.valueOf(resGameDetails.data.issue);
        kuai8Onet.setText(strarray[0]);
        kuai8Twot.setText(strarray[1]);
        kuai8Threet.setText(strarray[2]);
        kuai8Fourt.setText(strarray[3]);
        kuai8Fivt.setText(strarray[4]);
        kuai8Sixt.setText(strarray[5]);
        kuai8Sevent.setText(strarray[6]);
        kuai8Eightt.setText(strarray[7]);
        kuai8Noont.setText(strarray[8]);
        kuai8Tent.setText(strarray[9]);

        if (-1 != resGameDetails.data.n11) {
            niuniuTvOne.setText("庄" + niuniu[resGameDetails.data.n11]);
        } else {
            niuniuTvOne.setText("庄" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n12) {
            niuniuTvTwo.setText("闲一" + niuniu[resGameDetails.data.n12]);
        } else {
            niuniuTvTwo.setText("闲一" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n13) {
            niuniuTvThree.setText("闲二" + niuniu[resGameDetails.data.n13]);
        } else {
            niuniuTvThree.setText("闲二" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n14) {
            niuniuTvFour.setText("闲三" + niuniu[resGameDetails.data.n14]);
        } else {
            niuniuTvFour.setText("闲三" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n15) {
            niuniuTvFiv.setText("闲四" + niuniu[resGameDetails.data.n15]);
        } else {
            niuniuTvFiv.setText("闲四" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n16) {
            niuniuTvSix.setText("闲五" + niuniu[resGameDetails.data.n16]);
        } else {
            niuniuTvSix.setText("闲五" + niuniu[0]);
        }


        if (resGameDetails.data.winPlayer1.equals("1")) {
            niuniuTvTwo.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvTwo.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvTwo.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvTwo.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer2.equals("1")) {
            niuniuTvThree.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvThree.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvThree.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvThree.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer3.equals("1")) {
            niuniuTvFour.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvFour.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvFour.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvFour.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer4.equals("1")) {
            niuniuTvFiv.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvFiv.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvFiv.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvFiv.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer5.equals("1")) {
            niuniuTvSix.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvSix.setTextColor(getContext().getResources().getColor(R.color.settingbg));

        } else {
            niuniuTvSix.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvSix.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));

        }
        //庄
        if (resGameDetails.data.winPlayer5.equals("0") && resGameDetails.data.winPlayer4.equals("0")
                && resGameDetails.data.winPlayer3.equals("0") && resGameDetails.data.winPlayer2.equals("0")
                && resGameDetails.data.winPlayer1.equals("0")) {
            niuniuTvOne.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
        } else {
            niuniuTvOne.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
        }
        if (!TextUtils.isEmpty(resGameDetails.data.issue.toString()) && !TextUtils.isEmpty(resGameDetails.data.preIssue.toString())) {
            long b = Long.parseLong(resGameDetails.data.issue.toString()) - Long.parseLong(resGameDetails.data.preIssue.toString());
            if (b != 1&& b  < 3) {
                mPresenter.getgamesdetail1(gameid);
                return;
            }
        }
        //请求余额刷新
        if (null != Info.user) {
            mPresenter.getmoney(Info.user.token);
        }
    }

    public void Lottery1(ResGameDetails resGameDetails) {
        GameDetailActivityFinal.webvideo = resGameDetails.data.videoUrl;
//        webvideo = resGameDetails.data.videoUrl;
        niuniuUpTv.setPadding(20, 0, 0, 0);
        niuniuUpTv.setText(resGameDetails.data.preIssue + "期");
        periodDownTv.setText(resGameDetails.data.issue + "期");
        if (!TextUtils.isEmpty(resGameDetails.data.issue.toString()) && !TextUtils.isEmpty(resGameDetails.data.preIssue.toString())) {
            long b = Long.parseLong(resGameDetails.data.issue.toString()) - Long.parseLong(resGameDetails.data.preIssue.toString());
            if (b != 1&& b  < 3) {
                if (timer1 != null) {
                    timer1.cancel();
                    timer1 = null;
                }
                if (timer2 != null) {
                    timer2.cancel();
                    timer2 = null;
                }
                mPresenter.getgamesdetail(gameid);
                return;
            }
        }
        String[] strarray = resGameDetails.data.preNum.split(",");
        dataid = String.valueOf(resGameDetails.data.issue);
        kuai8Onet.setText(strarray[0]);
        kuai8Twot.setText(strarray[1]);
        kuai8Threet.setText(strarray[2]);
        kuai8Fourt.setText(strarray[3]);
        kuai8Fivt.setText(strarray[4]);
        kuai8Sixt.setText(strarray[5]);
        kuai8Sevent.setText(strarray[6]);
        kuai8Eightt.setText(strarray[7]);
        kuai8Noont.setText(strarray[8]);
        kuai8Tent.setText(strarray[9]);

        if (-1 != resGameDetails.data.n11) {
            niuniuTvOne.setText("庄" + niuniu[resGameDetails.data.n11]);
        } else {
            niuniuTvOne.setText("庄" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n12) {
            niuniuTvTwo.setText("闲一" + niuniu[resGameDetails.data.n12]);
        } else {
            niuniuTvTwo.setText("闲一" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n13) {
            niuniuTvThree.setText("闲二" + niuniu[resGameDetails.data.n13]);
        } else {
            niuniuTvThree.setText("闲二" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n14) {
            niuniuTvFour.setText("闲三" + niuniu[resGameDetails.data.n14]);
        } else {
            niuniuTvFour.setText("闲三" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n15) {
            niuniuTvFiv.setText("闲四" + niuniu[resGameDetails.data.n15]);
        } else {
            niuniuTvFiv.setText("闲四" + niuniu[0]);
        }
        if (-1 != resGameDetails.data.n16) {
            niuniuTvSix.setText("闲五" + niuniu[resGameDetails.data.n16]);
        } else {
            niuniuTvSix.setText("闲五" + niuniu[0]);
        }


        if (resGameDetails.data.winPlayer1.equals("1")) {
            niuniuTvTwo.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvTwo.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvTwo.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvTwo.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer2.equals("1")) {
            niuniuTvThree.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvThree.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvThree.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvThree.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer3.equals("1")) {
            niuniuTvFour.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvFour.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvFour.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvFour.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer4.equals("1")) {
            niuniuTvFiv.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvFiv.setTextColor(getContext().getResources().getColor(R.color.settingbg));
        } else {
            niuniuTvFiv.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvFiv.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));
        }
        if (resGameDetails.data.winPlayer5.equals("1")) {
            niuniuTvSix.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
            niuniuTvSix.setTextColor(getContext().getResources().getColor(R.color.settingbg));

        } else {
            niuniuTvSix.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
            niuniuTvSix.setTextColor(getContext().getResources().getColor(R.color.dodgerblue));

        }
        //庄
        if (resGameDetails.data.winPlayer5.equals("0") && resGameDetails.data.winPlayer4.equals("0")
                && resGameDetails.data.winPlayer3.equals("0") && resGameDetails.data.winPlayer2.equals("0")
                && resGameDetails.data.winPlayer1.equals("0")) {
            niuniuTvOne.setBackground(getResources().getDrawable(R.drawable.bt_backgroud));//黄色
        } else {
            niuniuTvOne.setBackground(getResources().getDrawable(R.drawable.bt_backgroud1));
        }

    }

    CountDownTimer timer1;
    CountDownTimer timer2;

    //封盘倒计时
    public void startleftTime(final String startTime, final String nowTime) {
        String mStartTile = startTime;
        String mNowTile = nowTime;
        Date startDate = DateUtils.strToDateLong(mStartTile);
        Date nowDate = DateUtils.strToDateLong(mNowTile);
        final long diff1 = startDate.getTime() - nowDate.getTime() + 500;
        if(diff1-500>0){
            timer1 = new CountDownTimer(diff1, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    xiazhubt.setEnabled(true);
                    chongzhibt.setEnabled(true);
                    touzhued.setEnabled(true);
                    fengpanlin.setVisibility(View.GONE);
                    closeTimeTv.setText(TimeTools.getCountTimeByLong(millisUntilFinished));
                }

                @Override
                public void onFinish() {
                    if (timer1 != null) {
                        timer1.cancel();
                    }
                    closeTimeTv.setText(String.valueOf("已封盘"));
                    fengpanlin.setVisibility(View.VISIBLE);
                    keybord.setVisibility(View.GONE);
                    xiazhubt.setEnabled(false);
                    chongzhibt.setEnabled(false);
                    touzhued.setEnabled(false);
                }
            };
            timer1.start();
        }
    }


    public void startrightTime(final String startTime, final String nowTime, final int a) {
        String mStartTile = startTime;
        String mNowTile = nowTime;
        Date startDate = DateUtils.strToDateLong(mStartTile);
        Date nowDate = DateUtils.strToDateLong(mNowTile);
        final long diff = startDate.getTime() - nowDate.getTime() + 500;
        if(diff-500>0){
            timer2 = new CountDownTimer(diff, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    openTimeTv.setText(TimeTools.getCountTimeByLong(millisUntilFinished));
                    if (TimeTools.getCountTimeByLong(millisUntilFinished).equals("00:00:00")) {
                        openTimeTv.setText("开奖中");
                    }
                }

                @Override
                public void onFinish() {
                    if (timer2 != null) {
                        timer2.cancel();
                    }
                    openTimeTv.setText("开奖中");
                    SystemClock.sleep(2000);//休眠2秒，保证可以获取到最新的开奖数据
                    mPresenter.getgamesdetail(gameid);
                    //接着请求钱的接口
                }
            };
            timer2.start();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (timer1 != null) {
            timer1.cancel();
            timer1 = null;
        }
        if (timer2 != null) {
            timer2.cancel();
            timer2 = null;
        }

    }
}

